import os

try:
    from matplotlib import cm
except:
    raise ImportError('The cm library cannot be found!')
try:    
    import matplotlib.pyplot as plt
except:
    raise ImportError('The matplotlib.pyplot library cannot be found!')
    
try:
    import pylab
except:
    raise ImportError('The pylab library cannot be found!')
    
try:
    import numpy as np
except:
    raise ImportError('The numpy library cannot be found!')

try:
    from cdar.models.Shallow_Water_2D.ShallowWater2DModel import ShallowWaterModelClass as model
except:
    raise ImportError('The model library cannot be found!')

try:    
    from cdar.utilities import namelist_read as nml
except:
    raise ImportError('The nml library cannot be found!')

EnSize = 100
SampleTime = 1800
left = 0
right   = 7500    
bottom = 7500
top   = 0    
  
DataFilePath='./data'  # Path to store or read FG, B and OBS

# Read in parameters for model configurations
params = nml.namelist_read(DataFilePath)

# Set up the model   
domain = model(params)  

# Read B file                                                       
#BFileName = os.path.join(DataFilePath,'B_'+domain.Name+'_'+str(EnSize)+'_'+str(domain.nx)+'_'+str(SampleTime)+'.npz')
BFileName = os.path.join(DataFilePath,'B_'+domain.Name+'_'+'100'+'_'+str(domain.nx)+'_'+str(SampleTime)+'.npz')
print "Reading the BE from file: %s" %  BFileName     
npzfile=np.load(BFileName)
Binv = npzfile['B']

# Start to plot
fig = pylab.figure() 
ax = fig.add_subplot(111)
ax.set_title('B Inverse') 

#Binv *= (1.0/Binv.max())

im = plt.imshow(Binv, interpolation='bilinear', cmap=cm.RdYlGn, \
                    vmax=abs(Binv).max()/100., vmin=-abs(Binv).max()/100.)
fig.colorbar(im) 
plt.show()